﻿using BCCommon.Enums;
using BCData.WeChatComponent.WeChatMessageRecord;
using BCEntity.WeChatComponent.WeChatMessageRecord;
using System;
using System.Collections.Generic;
using System.Text;
using WeChatSDK.WeChatDto.Conversation.ReceivingMessage;

namespace BCService.WeChatComponent.WeChatHandleService.VoiceMessageService
{
    /// <summary>
    /// 微信语音消息处理
    /// </summary>
    public class VoiceMessageService : IWeChatMessageHandle
    {
        private readonly IWeChatMessageRecordData weChatMessageRecordData;

        public VoiceMessageService(IWeChatMessageRecordData weChatMessageRecordData)
        {
            this.weChatMessageRecordData = weChatMessageRecordData;
        }

        public string HandleMessage(ReceivingMessageDto message)
        {
            VoiceMessageDto voiceMessageDto = (VoiceMessageDto)message;
            WeChatMessageRecordEntity weChatMessageRecordEntity = this.RecordReceivingMessage(voiceMessageDto);
            string response = "success";
            return response;
        }

        private WeChatMessageRecordEntity RecordReceivingMessage(VoiceMessageDto voiceMessageDto)
        {
            WeChatMessageRecordEntity entity = new WeChatMessageRecordEntity()
            {
                ToUserName = voiceMessageDto.ToUserName,
                FromUserName = voiceMessageDto.FromUserName,
                MessageType = WeChatMessageType.Voice,
                MessageId = voiceMessageDto.MsgId,
                MediaId = voiceMessageDto.MediaId,
                Fomat = voiceMessageDto.Format,
                IsReply = false
            };
            if (string.IsNullOrWhiteSpace(voiceMessageDto.Recognition))
            {
                entity.Recognition = voiceMessageDto.Recognition;
            }
            return this.weChatMessageRecordData.Add(entity);
        }
    }
}
